﻿using System;
using System.Text;
using System.IO;
using Tilver.Coder.Core;
using Tilver.Coder.Utility;

namespace Tilver.Coder.Engine
{
    public abstract class EngineBase : IEngine
    {
        #region IEngine 成员
        /// <summary>
        /// 生成代码样式
        /// </summary>
        public DBTextStyle.DBTextStyleBase DBTextStyle { get; set; }
        /// <summary>
        /// 命名空间
        /// </summary>
        public string NameSpace { get; set; }
        /// <summary>
        /// 输入Tab字符个数
        /// </summary>
        public int Tabs { get; set; }
        /// <summary>
        /// 生成代码
        /// </summary>
        /// <returns></returns>
        public abstract string RenderToString();
        /// <summary>
        /// 生成代码文件
        /// </summary>
        /// <param name="path"></param>
        public void RenderToFile(string path)
        {
            string directoryName = Path.GetDirectoryName(path);
            if (directoryName != null && !Directory.Exists(directoryName))
            {
                Directory.CreateDirectory(directoryName);
            }
            using (var writer = new StreamWriter(path, false, Encoding.UTF8))
            {
                writer.Write(RenderToString());
            }
        }
        #endregion

        /// <summary>
        /// 输出引入的命名空间
        /// </summary>
        /// <param name="response"></param>
        /// <param name="tabs"></param>
        protected void ResponseNameSpaces(StringBuilder response, int tabs)
        {
            response.AppendTabLine(tabs, "//==========================================================");
            response.AppendTabLine(tabs, "// 此代码由TilverNET代码生成工具自动生成，请不要修改");
            response.AppendFormatLine(tabs, "// 代码生成器版本号：{0}", ExtensionUtil.GetAssemblyVersion());
            response.AppendFormatLine(tabs, "// 作者：{0}", "landy");
            response.AppendFormatLine(tabs, "// 联系方式：{0}", "(QQ)86566178");
            response.AppendFormatLine(tabs, "// 创建时间：{0}", DateTime.Now);
            response.AppendTabLine(tabs, "//==========================================================");
            response.AppendLine();
            response.AppendTabLine(tabs, "using System;");
            response.AppendTabLine(tabs, "using System.Text;");
            response.AppendTabLine(tabs, "using System.Data;");
            response.AppendTabLine(tabs, "using System.Data.Common;");
            response.AppendTabLine(tabs, "using System.Collections.Generic;");
            response.AppendTabLine(tabs, "using System.Linq;");
            if (DBTextStyle.DataProviderType == DataProviderTypeEnum.MySQL)
            {
                response.AppendTabLine(tabs, "using MySql.Data;");
            }
            //response.AppendTabLine(tabs, "using System.ComponentModel;");
            //response.AppendTabLine(tabs, "using System.Runtime.Serialization;");
            //response.AppendTabLine(tabs, "using System.Security.Permissions;");
            //response.AppendTabLine(tabs, "using System.Xml;");
            //response.AppendTabLine(tabs, "using Tilver.Common;");
            response.AppendTabLine(tabs, "using Tilver.Data;");
            response.AppendTabLine(tabs, "using Tilver.Data.DataPager;");
        }
    }
}
